package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.接收品牌数据 (客户端使用post请求,以json字符串形式发送过来)
        String json = request.getReader().readLine();
        Brand brand = JSON.parseObject(json, Brand.class);

        // 2.调用service添加
        BrandService brandService = new BrandServiceImpl();
        brandService.add(brand);

        // 3.响应成功标识: "success"
        response.getWriter().write("success");
    }
}
